package com.glory.dmegcpda.view

import android.os.Bundle
import android.os.Handler
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.View
import com.glory.dmegcpda.R
import com.glory.dmegcpda.bean.ScriBingCaseBean
import com.glory.dmegcpda.bean.ScrinBingBoxBean
import com.glory.dmegcpda.bean.ScrinBingEqpBean
import com.glory.dmegcpda.viewmodel.ScribingViewModel
import com.glory.mvvmnet.base.BaseActivity
import com.glory.mvvmnet.entity.ToastMessageBean
import kotlinx.android.synthetic.main.activity_scribing.*

class ScribingActivity : BaseActivity<ScribingViewModel>() {

    override fun layoutId(): Int {
        return R.layout.activity_scribing
    }

    override fun initData() {

        viewModel.defUI.callObserve.observe(this) {
            when (it.type) {
                1 -> {
                    eqpCode.requestFocus()
                    val caseBean = it.item as ScriBingCaseBean
                    case_line.visibility = View.VISIBLE
                    case_nameText.text = caseBean.dATALIST.get(0).mATERIALNAME
                    case_startText.text = caseBean.dATALIST.get(0).sTATE
                    case_numText.text = caseBean.dATALIST.get(0).mAINQTY.toString()
                    case_contentText.text = caseBean.dATALIST.get(0).mATERIALDESC
                }
                2 -> {
                    boxCode.requestFocus()
                    val eqpBean = it.item as ScrinBingEqpBean
                    eqp_line.visibility = View.VISIBLE
                    eqp_nameText.text = eqpBean.dATALIST.get(0).pOSITIONNAME
                    eqp_typeText.text = eqpBean.dATALIST.get(0).mATERIALTYPESRC
                    eqp_contentText.text = eqpBean.dATALIST.get(0).pOSITIONDESC
                }
                3 -> {
                    val boxBean = it.item as ScrinBingBoxBean
                    box_line.visibility = View.VISIBLE
                    box_nameText.text = boxBean.dATALIST[0].dURABLEID
                    box_startText.text = boxBean.dATALIST.get(0).sTATE
                    box_holdStartText.text = boxBean.dATALIST.get(0).hOLDSTATE
                    box_numText.text = boxBean.dATALIST.get(0).cURRENTQTY
                    box_capacityText.text = boxBean.dATALIST.get(0).cAPACITY
                }
                4 ->{
                    tipsText.text = "操作成功"
                }
            }
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        return_image.setOnClickListener{
            finish()
        }
        caseCode.addTextChangedListener(object :TextWatcher{
            override fun beforeTextChanged(text: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

            override fun onTextChanged(text: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

            override fun afterTextChanged(text: Editable?) {
                val toString = text.toString()
                if (toString.indexOf("\r") >= 0 || toString.indexOf("\n") >= 0) {
                    caseCode.setText(toString.replace("\r", "").replace("\n", ""))
                    viewModel.initCaseData(toString.replace("\r", "").replace("\n", ""))
                }
            }
        })

        eqpCode.addTextChangedListener(object :TextWatcher{
            override fun beforeTextChanged(text: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

            override fun onTextChanged(text: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

            override fun afterTextChanged(text: Editable?) {
                val toString = text.toString()
                if (toString.indexOf("\r") >= 0 || toString.indexOf("\n") >= 0) {
                    eqpCode.setText(toString.replace("\r", "").replace("\n", ""));
                    viewModel.initEqpData(toString.replace("\r", "").replace("\n", ""))
                }
            }
        })

        boxCode.addTextChangedListener(object :TextWatcher{
            override fun beforeTextChanged(text: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

            override fun onTextChanged(text: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

            override fun afterTextChanged(text: Editable?) {
                val toString = text.toString()
                if (toString.indexOf("\r") >= 0 || toString.indexOf("\n") >= 0) {
                    boxCode.setText(toString.replace("\r", "").replace("\n", ""));
                    viewModel.initBoxData(toString.replace("\r", "").replace("\n", ""))
                }
            }

        })

        button.setOnClickListener{
            if (caseCode.text.toString().equals("")) {
                viewModel.defUI.toastEvent.value = ToastMessageBean("请输入箱号条码",false)
                return@setOnClickListener
            }
            if (eqpCode.text.toString().equals("")){
                viewModel.defUI.toastEvent.value = ToastMessageBean("请输入设备条码",false)
                return@setOnClickListener
            }
            if (boxCode.text.toString().equals("")){
                viewModel.defUI.toastEvent.value = ToastMessageBean("请输入盒子条码",false)
                return@setOnClickListener
            }
            viewModel.initData(
                caseCode.text.toString(),
                eqpCode.text.toString(),
                boxCode.text.toString()
            )
        }
    }

}